//
//  NewHotViewCell.m
//  Hxcsfw
//
//  Created by 企友软mac on 2018/2/12.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "NewHotViewCell.h"
#import "MJExtension.h"
#import "HotLbtModel.h"
#import "UIImageView+WebCache.h"
#import "CollectionBtnCell.h"
#import "PCShopSlideRound.h"

@interface NewHotViewCell ()<UIScrollViewDelegate>{

    NSArray *arrTitle;
    NSArray *arrImg;
}

@property (weak, nonatomic) IBOutlet UIView *searchView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) PCShopSlideRound *scrPageControl;
@property (weak, nonatomic) IBOutlet UIView *scrollViewCon;

@end

@implementation NewHotViewCell

- (void)creatScrollViewBtn {
    // 1.创建scrollView
    // 2.添加图片到scrollView中
    CGFloat scrollW = _scrollView.bounds.size.width;
    CGFloat scrollH = _scrollView.bounds.size.height;

    NSArray *arrayOfImages = @[@"ys_15.png",@"hot_wenba.png",@"hot_wentifenlei.png",@"zaixianyueke.png",@"shouyewangshangjiaofei.png",@"hot_QR.png",@"hot_more2.png"];
     NSArray *arrTitle = @[@"在线客服",@"图解问吧",@"问题分类",@"在线约课",@"会员缴费",@"扫码查验",@"更多服务",];
//    NSArray *arrayOfImages = @[@"hot_picture.png",@"ys_15.png",@"hot_wenba.png",@"hot_wentifenlei.png",@"zaixianyueke.png",@"shouyewangshangjiaofei.png",@"hot_QR.png",@"hot_more2.png"];
//    NSArray *arrTitle = @[@"手机拍服务",@"在线客服",@"图解问吧",@"问题分类",@"在线约课",@"会员缴费",@"扫码查验",@"更多服务",];

    UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WSCREENH_WIDTH * 2, scrollH - 15)];
    for (int i = 0; i < 7; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(i * WSCREENH_WIDTH / 4., 0, WSCREENH_WIDTH / 4., scrollH - 15);
        btn.tag = i;
        [btn addTarget:self action:@selector(scrollBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arrayOfImages[i]]];
        imgView.frame = CGRectMake((btn.width - 25)*0.5, 15, 25, 25);
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [btn addSubview:imgView];

        UILabel *lbTit = [[UILabel alloc] initWithFrame:CGRectMake(0, btn.height - 16, btn.width, 16)];
        lbTit.text = arrTitle[i];
        lbTit.textAlignment = NSTextAlignmentCenter;
        lbTit.font = [UIFont systemFontOfSize:12];
        [btn addSubview:lbTit];

        [btnView addSubview:btn];
    }
    [_scrollView addSubview:btnView];

    // 3.设置scrollView属性
    _scrollView.contentSize = CGSizeMake(2 * WSCREENH_WIDTH, 0);
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;

    // 4.添加pageControl，用于分页
    self.scrPageControl = [[PCShopSlideRound alloc] initWithFrame:CGRectMake((WSCREENH_WIDTH-90)/2, _scrollView.height*0.9, 90, 15) andRoundCount:2];

    self.scrPageControl.spaceWithRound =8.0f;
    self.scrPageControl.sizeOfRound =CGSizeMake(8.0f, 3.0f);
    self.scrPageControl.imageOfUnSelected =[UIImage imageNamed:@"shouyexiaoqiehuan1.png"];
    self.scrPageControl.imageOfSelected =[UIImage imageNamed:@"shouyexiaoqiehuan2.png"];
    self.scrPageControl.alignment =SlideRoundAlignmentCenter;
    [self.scrollViewCon addSubview:self.scrPageControl];
}

- (void)scrollBtnClick:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatiCarClick:)]){
        [self.delegate delegatCollectionBtnClick:btn.tag];
    }
}

#pragma mark - UIScrollViewDelegate
// 该代理方法能监听当前scroll的更多属性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    double page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 四舍五入计算出页码
    // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
//    self.scrPageControl.currentPage = (int)(page + 0.5);
    CGFloat pageWidth = WSCREENH_WIDTH;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    [self.scrPageControl SlideRoundSelectIndex:page];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    _lbTypeT.hidden = YES;
    
    _lbRemind.hidden = YES;
    _topContract.constant = 0;

    _imgTop.layer.masksToBounds = YES;
    _imgPolicy.layer.masksToBounds = YES;
    _imgReport.layer.masksToBounds = YES;

    _imgTop.layer.cornerRadius = 6;
    _imgPolicy.layer.cornerRadius = 6;
    _imgReport.layer.cornerRadius = 6;
    
//    _searchView.backgroundColor = WRGBColor(246, 246, 246);
//    _searchView.layer.cornerRadius = 15;
//    _searchView.layer.borderColor = WRGBColor(228, 228, 228).CGColor;
//    _searchView.layer.borderWidth = 1.f;
//    _searchView.layer.masksToBounds = YES;
    _arrayOfAds = [NSMutableArray array];
    [_arrayOfAds removeAllObjects];

    for (NSDictionary *dic in [WShareUtil sharedData].arrayOfAds){
        HotLbtModel *model= [HotLbtModel mj_objectWithKeyValues:dic];
        [_arrayOfAds addObject:model];
    }

    _iCarouselView.type = iCarouselTypeCylinder;
    _iCarouselView.decelerationRate =0.4;
    _iCarouselView.scrollSpeed =0.4;
    _iCarouselView.delegate = self;
    _iCarouselView.dataSource = self;

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.iCarouselView.frame.size.height-20, self.iCarouselView.frame.size.width, 20)];

    pageControl.numberOfPages = _arrayOfAds.count;

    CGSize pointSize = [pageControl sizeForNumberOfPages:_arrayOfAds.count];

    CGFloat page_x = -(pageControl.bounds.size.width - pointSize.width) / 2 ;

    [pageControl setBounds:CGRectMake(page_x + 15, pageControl.bounds.origin.y,
                                      pageControl.bounds.size.width, pageControl.bounds.size.height)];
    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    pageControl.pageIndicatorTintColor = [UIColor brownColor];
    pageControl.userInteractionEnabled = NO;

    self.pageControl = pageControl;

    [self.iCarouselView addSubview:self.pageControl];

//    if (!PCReleaseFlag) {
//        UIButton *flagBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
//        flagBtn.frame = CGRectMake(WSCREENH_WIDTH - 50, 10, 40, 40);
//        [flagBtn addTarget:self action:@selector(flagAppUrl) forControlEvents:UIControlEventTouchUpInside];
//        [self.iCarouselView addSubview:flagBtn];
//    }

    [self addTimer];

//    arrTitle = @[@"手机拍服务",@"在线客服",@"图解问吧",@"问题分类",@"扫码查验",@"更多服务"];
//    arrImg = @[@"hot_picture.png",@"ys_15.png",@"hot_wenba.png",@"hot_wentifenlei@2x.png",@"hot_QR.png",@"hot_more2.png"];

    [self creatScrollViewBtn];
}

//- (void)flagAppUrl {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatFlagAppUrlBtnClick)]){
//        [self.delegate delegatFlagAppUrlBtnClick];
//    }
//}

-(void)addTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(nextCarousel) userInfo:nil repeats:YES];
}

//自动播放下一条广告功能
-(void)nextCarousel{
    NSInteger page=0;
    if (self.pageControl.currentPage == self.pageControl.numberOfPages-1) {
        page=0;
    }else{
        page=self.pageControl.currentPage+1;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3f];

    self.pageControl.currentPage=page;
    //设置icarouselAdView
    self.iCarouselView.currentItemIndex=page;
    [UIView commitAnimations];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (IBAction)btnClick:(UIButton *)sender {

    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatBtnClick:)]){
        [self.delegate delegatBtnClick:sender];
    }
}

- (void)dealloc {
    if (self.timer) {
        [self.timer invalidate];
        self.timer=nil;
    }
}

#pragma mark -iCarouselDataSource&&iCarouselDelegate

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{

    return [_arrayOfAds count];
}

- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel{

    return 0;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel{

    return carousel.frame.size.width;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{

    UIImageView *imageView;

    if (carousel == _iCarouselView) {
        if(view == nil){
            view=[[UIView alloc]initWithFrame:self.iCarouselView.bounds];
            imageView=[[UIImageView alloc]initWithFrame:view.bounds];
            [view addSubview:imageView];
        }
        else{
            imageView =[[view subviews] lastObject];
        }

        if ([_arrayOfAds[index] isKindOfClass:[HotLbtModel class]]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_arrayOfAds[index] imgsrc]] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];

        }else {
            imageView.image = [UIImage imageNamed:@"placeholderImage.png"];
        }
    }
    return view;
}

//点击事件
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegatiCarClick:)]){
        [self.delegate delegatiCarClick:index];
    }
}

-(void)carouselDidScroll:(iCarousel *)carousel{
    self.pageControl.currentPage = carousel.currentItemIndex;
}

- (void)carouselWillBeginDragging:(iCarousel *)carousel{
    [self.timer invalidate];
    self.timer=nil;
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

@end
